package com.avic.gateway.filter;

import com.avic.gateway.common.config.properties.FilterRuleProperties;
import com.avic.gateway.common.constant.GatewayConstant;
import com.avic.gateway.common.util.RequestUtil;
import com.avic.gateway.common.util.SpringContextUtil;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

import java.util.List;
import java.util.Map;

/**
 * 全局过滤器，设置特定的URL经过特定的过滤器
 *
 * @author: huangsheng
 * @date: 2018/8/24
 */
@Slf4j
@EnableConfigurationProperties({FilterRuleProperties.class})
public class UriRulePreFilter extends AbstractZuulFilter {

    private FilterRuleProperties filterRuleProperties = SpringContextUtil.getBean(FilterRuleProperties.class);

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        // 设置response body为 json
        requestContext.getResponse().setContentType(GatewayConstant.DEFAULT_RESPONSE_CONTENT_TYPE);
        // 设置请求为200
        requestContext.setResponseStatusCode(200);

        String requestUri = requestContext.getRequest().getRequestURI();
        log.info("request uri {}", requestUri);

        //  设置每个链接都需要进入的过滤器规则
        requestContext.set(FailedRequestPostFilter.class.getSimpleName(), true);
        requestContext.set(ResponseBodyPostFilter.class.getSimpleName(), true);

        Map<String, String> ruleMap = filterRuleProperties.getRuleMap();
        if (ruleMap.containsKey(requestUri)) {
            log.info("setting request uri {} should filter {}", requestUri, ruleMap.get(requestUri));
            requestContext.set(ruleMap.get(requestUri), true);
        }

        // 设置通过Token过滤器
        requestContext.set(TokenValidatePreFilter.class.getSimpleName(), true);
        List<String> ignoreTokenUri = filterRuleProperties.getIgnoreTokenUri();
        ignoreTokenUri.forEach(uri -> {
            if (requestUri.startsWith(uri)) {
                log.info("setting request uri {} ignore token filter {}", requestUri, ruleMap.get(requestUri));
                requestContext.set(TokenValidatePreFilter.class.getSimpleName(), false);
            }
        });

        List<String> downloadUri = filterRuleProperties.getDownloadUri();
        downloadUri.forEach(uri -> {
            if (requestUri.startsWith(uri)) {
                log.info("setting request uri {} ignore FailedRequestPostFilter filter", requestUri);
                requestContext.set(FailedRequestPostFilter.class.getSimpleName(), false);
                requestContext.set(LoginCreateTokenPostFilter.class.getSimpleName(), false);
                requestContext.set(LogOutRemoveTokenPostFilter.class.getSimpleName(), false);
                requestContext.set(ResponseBodyPostFilter.class.getSimpleName(), false);
                requestContext.set(TokenValidatePreFilter.class.getSimpleName(), false);
            }
        });

        // 设置请求头
        String remoteAddr = RequestUtil.getIpAddress(requestContext.getRequest());
        requestContext.addZuulRequestHeader(GatewayConstant.HEADER_X_REAL_IP, remoteAddr);

        return null;
    }
}
